<template>
  <el-form
    :model="healthtable"
    class="healthtable-info"
    :rules="rules"
    ref="healthtable"
  >
    <div>
      <div class="healthtable-head">
        <h3 class="healthtable-head-text">居民健康登记表</h3>
      </div>
      <div class="healthtable-title">
        <p class="healthtable-title-text">健康登记</p>
      </div>
    </div>
    <!-- 修改个人信息 -->
    <div
      style="padding-left:5px; margin: 50px; margin-left: 150px; width: 600px;"
    >
      <el-form-item
        class="Info-change"
        prop="infectstatus"
        label="健康状况"
        label-width="120px"
      >
        <el-radio-group v-model="healthtable.infectstatus">
          <el-radio label="正常"></el-radio>
          <el-radio label="未发热且隔离"></el-radio>
          <el-radio label="发热且隔离"></el-radio>
          <el-radio label="疑似"></el-radio>
          <el-radio label="确诊"></el-radio>
          <el-radio label="疑似转排除"></el-radio>
          <el-radio label="已治愈"></el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        class="Info-change"
        prop="medicalcondition"
        label="就医情况"
        label-width="120px"
      >
        <el-radio-group
          :disabled="notInfected"
          v-model="healthtable.medicalcondition"
        >
          <el-radio label="已就医"></el-radio>
          <el-radio label="未就医"></el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        class="Info-change"
        prop="temprature"
        label="体温"
        label-width="120px"
      >
        <el-input
          v-model="healthtable.temprature"
          placeholder="请输入体温"
        ></el-input>
      </el-form-item>

      <el-form-item
        class="Info-change"
        prop="livingstatus"
        label="居住状况"
        label-width="120px"
        label-height="100px"
      >
        <el-radio-group v-model="healthtable.livingstatus">
          <el-radio label="在家"></el-radio>
          <el-radio label="未在家"></el-radio>
        </el-radio-group>
      </el-form-item>

      <el-button
        style="margin-left: 200px;"
        type="primary"
        @click="onSubmited('healthtable')"
        >修改</el-button
      >
      <el-button style="margin-left: 100px;" @click="onReset('healthtable')"
        >清空</el-button
      >
    </div>
  </el-form>
</template>

<script>
export default {
  name: "healthtableSubmit",
  //返回新生成的组件相关数据
  data() {
    var checkInfected = (rule, value, callback) => {
      if (
        this.healthtable.infectstatus == "正常" ||
        this.healthtable.infectstatus == "已治愈"
      ) {
        return callback();
      }
      else if(!value)
        return callback(new Error("请进行选择"));
      return callback();
    };

    return {
      healthtable: {
        infectstatus: "",
        temprature: "",
        livingstatus: "",
        medicalcondition: "",
      },
      //定义表单验证的规则
      rules: {
        infectstatus: [
          { required: true, message: "请进行选择", trigger: "blur" },
        ],
        temprature: [
          { required: true, message: "请进行选择", trigger: "blur" },
        ],
        livingstatus: [
          { required: true, message: "请进行选择", trigger: "blur" },
        ],
        medicalcondition: [{ validator: checkInfected, trigger: ["blur"] }],
      },
    };
  },

  //前端测试路由拦截器，查看发送的请求正确与否
  /*mounted(){
  		this.axios.interceptors.request.use(config => {
  			console.log(config)
  			return config
  		},error =>{
  			return Promise.reject(error)
  		})
    },*/
  computed: {
    notInfected() {
      if (
        this.healthtable.infectstatus == "正常" ||
        this.healthtable.infectstatus == "已治愈"
      )
        return true;
      else return false;
    },
  },

  methods: {
    //修改信息事件
    onSubmited(info) {
      //判断输入的数据是否符合表单规则
      this.$refs[info].validate((valid) => {
        if (valid) {
          if (
            this.healthtable.infectstatus == "正常" ||
            this.healthtable.infectstatus == "已治愈"
          )
            this.healthtable.infectstatus = 0;
          else this.healthtable.infectstatus = 1;
          if (this.healthtable.livingstatus == "在家")
            this.healthtable.livingstatus = 0;
          else this.healthtable.livingstatus = 1;

          //将希望修改的数据传送到后端
          this.$axios
            .post("/api/healthtable", this.healthtable, {
              headers: { Authorization: "Bearer " + this.$store.state.token },
            })
            .then((response) => {
              console.log(response);
              if (response.data) {
                this.alertBox("修改成功");
              } else {
                this.alertBox("修改失败");
              }
            })
            .catch((error) => {
              console.log(error);
            });
        } else {
          console.log("error!");
        }
      });
    },
    //初始化输入表单
    onReset(info) {
      this.$refs[info].resetFields();
    },
  },
};
</script>

<style>
/* 这里是个人信息的相关css表 */
.healthtable-info {
  padding-left: 10px;
}
/*定义了主体结构标题*/
.healthtable-head {
  width: 100px;
  height: 52px;
  border-bottom: 1px solid rgb(224, 224, 224);
}
/*定义了主体结构标题的文字格式*/
.healthtable-head-text {
  width: 260px;
  height: 52px;
  line-height: 29px;
  color: rgba(112, 112, 112, 1);
  font-size: 28px;
  font-family: SourceHanSansSC-bold;
}
/*定义了主体结构副标题*/
.healthtable-title {
  width: 1000px;
  height: 50px;
  line-height: 20px;
  padding-left: 15px;
  background-color: rgba(224, 224, 224, 1);
  border: 1px solid rgba(224, 224, 224, 1);
}
/*定义了主体结构副标题的文字格式*/
.healthtable-title-text {
  color: rgba(16, 16, 16, 1);
  font-size: 16px;
  font-family: SourceHanSansSC-regular;
}
</style>
